package czk.concrent.juc.countDOwnLatch.practice;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/10/23 15:34
 */
public class Flowerd implements Runnable {
    private CountDownLatch countDownLatch;

    @Override
    public void run() {
        try {
            TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(1000));
            System.out.println(Thread.currentThread().getName() + ":来了" + "还差" + (countDownLatch.getCount() - 1) + "个人");
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public Flowerd(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    public static void main(String[] args) {
        System.out.println("开始呼叫孩子们");
        CountDownLatch downLatch = new CountDownLatch(3);
        System.out.println("呼叫小明。。。");
        new Thread(new Flowerd(downLatch), "小明").start();
        System.out.println("呼叫小滑。。。");
        new Thread(new Flowerd(downLatch), "小滑").start();
        System.out.println("呼叫小六。。。");
        new Thread(new Flowerd(downLatch), "小六").start();
        System.out.println("孩子们呼叫完毕");
        try {
            downLatch.await();
            System.out.println("孩子们都来了，开始做游戏吧。。。。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
